package HeapSort;

import java.util.Random;

public class Sort
{
    public static void main(String[] args) {
        Random random = new Random();
        int[] b = new int[11];
        for (int i = 1; i < b.length; i++) {
            b[i] = random.nextInt(100);
        }
        for(int i=1;i<b.length;i++)
        {
            System.out.print(b[i]+"   ");
        }
        System.out.println();
        System.out.println(b.length);
        sort(b,b.length-1);
        for(int i=1;i<b.length;i++)
        {
            System.out.print(b[i]+"   ");
        }
    }
    public static void sort(int []a,int n)
    {
        int p=0;
        int tag=0;
        int end=n;
        while(end-1!=0)
        {
            while(true)
            {
               p=end/2;
               tag=0;
               while(p!=0)
               {
                   if(a[p]<a[2*p])
                   {
                       a[0]=a[p];
                       a[p]=a[2*p];
                       a[p*2]=a[0];
                       tag=1;
                   }
                   if(2*p+1<=end&&a[p]<a[2*p+1])
                   {
                       a[0]=a[p];
                       a[p]=a[2*p+1];
                       a[p*2+1]=a[0];
                       tag=1;
                   }
                   p--;
               }
               if(tag==0)
                   break;
            }
            a[0]=a[1];
            a[1]=a[end];
            a[end]=a[0];
            end--;
        }
//        for(int i=1;i<a.length;i++)
//        {
//            System.out.print(a[i]+"   ");
//        }
    }
}
